package caro.client.gui;
import caro.client.gui.templates.MyTextField;
import caro.client.gui.templates.AbstractWindow;
import caro.client.gui.templates.MyButton;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.*;
/**
 * Class ServerConnection
 * @author Thanh Nhan
 * @Since Aug 28, 2011
 * @Version 1.0
 */
public class ServerConnection extends AbstractWindow{
    private static final String SERVER_PORT = "2212";
    private static final String LOCAL_HOST = "localhost";
    private JTextField serverIp = null;
    private JTextField serverPort = null;
    
    private JButton connect = null;
    private JButton exit = null;
    
    public ServerConnection(){
        setTitle(Messages.getString("appTitle"));
        setResizable(false);
        WIN_W = 500;
        WIN_H = 200;
        BORDER_H = 2;
        setSize(WIN_W, WIN_H);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        setLayout(null);
        
        JLabel bgTitle = new JLabel(ImageProvider.getInstance().getImage("backgroundTitleShort.png"));
        bgTitle.setHorizontalAlignment(JLabel.LEFT);
        bgTitle.setLayout(null);
        
        JLabel title = new JLabel(Messages.getString("appTitle"));
        title.setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 20));
        title.setForeground(new Color(19, 58, 10));
        title.setBounds((WIN_W - CELL_W*9)/2, 0, CELL_W*9, CELL_H);
        
        JLabel subTitle = new JLabel(Messages.getString("connectServerTitle"));
        subTitle.setFont(new Font("Arial", Font.BOLD, 13));
        subTitle.setForeground(new Color(19, 58, 10));
        subTitle.setBounds(WIN_W - CELL_W*9, BORDER_H + CELL_H, CELL_W*9, CELL_H);
        
        bgTitle.setBounds(0, 0, WIN_W, 50);
        bgTitle.add(title);
        bgTitle.add(subTitle);
        
        add(bgTitle);
        
        JLabel ipLbl = new JLabel(Messages.getString("inputIpAddress"));
        ipLbl.setBounds(BORDER_W, bgTitle.getHeight() + 8*BORDER_H, 6*CELL_W, CELL_H);
        URL imgUrl = getClass().getResource("images/bgTextField.png");
        JLabel portLbl = new JLabel(Messages.getString("inputPort"));
        
        serverIp = new MyTextField(imgUrl);
        serverIp.setText(LOCAL_HOST);
        serverIp.setBorder(null);
        serverIp.setBounds(BORDER_W + ipLbl.getWidth() + BORDER_W, ipLbl.getY(), 7*CELL_W, ipLbl.getHeight());
        portLbl.setBounds(BORDER_W + ipLbl.getWidth() + BORDER_W 
                + serverIp.getWidth() + BORDER_W, ipLbl.getY(), 3*CELL_W, ipLbl.getHeight());
        serverPort = new MyTextField(imgUrl);
        serverPort.setText(SERVER_PORT);
        serverPort.setBorder(null);
        serverPort.setBounds(BORDER_W + ipLbl.getWidth() + BORDER_W 
                + serverIp.getWidth() + BORDER_W + portLbl.getWidth() + BORDER_W, ipLbl.getY(), 2*CELL_W, ipLbl.getHeight());
        add(ipLbl);
        add(serverIp);
        add(portLbl);
        add(serverPort);
        
        JLabel bgButton = new JLabel(ImageProvider.getInstance().getImage("bgButton.png"));
        bgButton.setBounds(0, WIN_H - 75, WIN_W, 50);
        bgButton.setLayout(null);
        
        connect = new MyButton(2*BORDER_W, 10*BORDER_H, 5*CELL_W, CELL_H, 
                ImageProvider.getInstance().getImage("button.png"), Messages.getString("connect"));
        connect.setFont(new Font("Arial", Font.BOLD, 13));
        exit = new MyButton(2*BORDER_W + connect.getWidth() + 2*BORDER_W, connect.getY(), 5*CELL_W, CELL_H, 
                ImageProvider.getInstance().getImage("button.png"), Messages.getString("exit"));
        exit.setFont(new Font("Arial", Font.BOLD, 13));
        bgButton.add(connect);
        bgButton.add(exit);
        
        add(bgButton);
    }

    /**
     * @return the serverIp
     */
    public String getServerIp() {
        return serverIp.getText();
    }

    /**
     * @return the serverPort
     */
    public String getServerPort() {
        return serverPort.getText();
    }
    
    public void addConnectListener(ActionListener a){
        connect.addActionListener(a);
    }
    public void addExitListener(ActionListener a){
        exit.addActionListener(a);
    }
}
